package com.team.RunsFast_Api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team.RunsFast_Api.dao.FoodDao;
import com.team.RunsFast_Api.dao.StoreDao;
import com.team.RunsFast_Api.dto.FoodAddDto;
import com.team.RunsFast_Api.dto.PositionDto;
import com.team.RunsFast_Api.service.intf.FoodService;
import com.team.runsfast.entity.Food;
import com.team.runsfast.entity.Store;
import com.team.runsfast.vo.R;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author fxy
 * @date 2022/9/24
 */
@Service
public class FoodServiceImpl implements FoodService {

    private FoodDao foodDao;
    private StoreDao storeDao;

    public FoodServiceImpl(FoodDao foodDao, StoreDao storeDao) {
        this.foodDao = foodDao;
        this.storeDao = storeDao;
    }

    //通过商铺的id查询所有的商品信息
    @Override
    public R selectBySid(Long id) {
        //查询指定店铺的商品信息
        List<Food> foods = foodDao.selectList(new QueryWrapper<Food>().eq("sid", id).eq("flag",1));
        HashMap<String,List<Object>> hashMap = new HashMap<>();
        Store store = storeDao.selectOne(new QueryWrapper<Store>().eq("id", id));
        List add = new ArrayList<>();
        add.add(store);
        hashMap.put("店铺",add);
        for(Food food:foods){
            //如果菜品类型存在，就添加
            String flag = getFlag(food.getTag());
            if(hashMap.containsKey(flag)){
                List f = hashMap.get(flag);
                f.add(food);
                hashMap.put(flag,f);
            }else {
                ArrayList list = new ArrayList<>();
                list.add(food);
                hashMap.put(flag,list);
            }
        }
        return R.ok(hashMap);
    }

    @Override
    public R addFood(FoodAddDto foodAddDto) {
        //Long sid, String title, Integer tag, Double orginprice, Double currentprice, String imgurl, String info, Double packprice
        int insert = foodDao.insert(new Food(foodAddDto.getSid(), foodAddDto.getTitle(), foodAddDto.getTag(), foodAddDto.getOrginprice(), foodAddDto.getCurrentprice(), foodAddDto.getImgurl(), foodAddDto.getInfo(), foodAddDto.getPackprice()));
        if(insert>0){
            return R.ok(null);
        }
        return R.fail();
    }

    //
    public String getFlag(Integer flag){
      switch (flag){
          //菜品类型（1. 汉堡披萨  2.便当快餐  3.奶茶饮品 4.地方菜 5.米粥 6.烧烤 7.西餐）
          case 1:
              return "汉堡披萨";
          case 2:
              return "便当快餐";
          case 3:
              return "奶茶饮品";
          case 4:
              return "地方菜";
          case 5:
              return "米粥";
          case 6:
              return "烧烤";
          case 7:
              return "西餐";
          case 8:
              return "主食";
      }
      return "精选";
    }
}
